{{!
  Copyright (c) HashiCorp, Inc.
  SPDX-License-Identifier: BUSL-1.1
~}}

<PageHeader as |p|>
  <p.top>
    <KmipBreadcrumb @showPath={{true}} />
  </p.top>
  <p.levelLeft>
    <h1 class="title is-3">
      {{this.scope}}
    </h1>
  </p.levelLeft>
</PageHeader>
<Toolbar>
  {{#if this.model.meta.total}}
    <ToolbarFilters>
      <NavigateInput
        @filterFocusDidChange={{action "setFilterFocus"}}
        @filterDidChange={{action "setFilter"}}
        @filter={{this.filter}}
        @filterMatchesKey={{this.filterMatchesKey}}
        @firstPartialMatch={{this.firstPartialMatch}}
        @placeholder="Filter roles by name"
        @extraNavParams={{this.scope}}
        @urls={{hash
          create="vault.cluster.secrets.backend.kmip.scope.roles.create"
          list="vault.cluster.secrets.backend.kmip.scope.roles"
          show="vault.cluster.secrets.backend.kmip.credentials"
        }}
      />
      {{#if this.filterFocused}}
        {{#if this.filterMatchesKey}}
          <p class="input-hint">
            <kbd>ENTER</kbd>
            to go to
            <code>{{this.filter}}</code>
            roles
          </p>
        {{/if}}
        {{#if this.firstPartialMatch}}
          <p class="input-hint">
            <kbd>TAB</kbd>
            to complete
            <code>{{this.firstPartialMatch.id}}</code>
          </p>
        {{/if}}
      {{/if}}
    </ToolbarFilters>
  {{/if}}
  <ToolbarActions>
    <ToolbarLink @route="scope.roles.create" @type="add" data-test-role-create>
      Create role
    </ToolbarLink>
  </ToolbarActions>
</Toolbar>
<ListView @items={{this.model}} @itemNoun="role" @paginationRouteName="scope.roles" as |list|>
  {{#if list.empty}}
    <list.empty
      @title="No roles in this scope yet"
      @message="Roles let you generate credentials with a specified set of KMIP operations permissions that clients are allowed to perform."
    >
      <LinkTo @route="scope.roles.create">
        Create a role
      </LinkTo>
    </list.empty>
  {{else if list.item}}
    <ListItem @linkPrefix={{this.mountPoint}} @linkParams={{array "credentials" this.scope list.item.id}} as |Item|>
      <Item.content>
        <Icon @name="user" class="has-text-grey-light" />{{list.item.id}}
      </Item.content>
      <Item.menu as |m|>
        <li class="action">
          <LinkTo @route="credentials" @models={{array this.scope list.item.id}} class="is-block">
            View credentials
          </LinkTo>
        </li>
        <li class="action">
          <LinkTo @route="role" @models={{array this.scope list.item.id}} class="is-block">
            View role
          </LinkTo>
        </li>
        {{#if list.item.updatePath.canUpdate}}
          <MenuLoader @loadingParam={{list.item.updatePath.isPending}}>
            <LinkTo @route="role.edit" @models={{array this.scope list.item.id}} class="is-block">
              Edit role
            </LinkTo>
          </MenuLoader>
        {{/if}}
        {{#if list.item.updatePath.canDelete}}
          <MenuLoader @loadingParam={{list.item.updatePath.isPending}}>
            <m.Message
              @id={{list.item.id}}
              @triggerText="Delete role"
              @message={{concat "Are you sure you want to delete " list.item.id "?"}}
              @onConfirm={{action
                (perform
                  Item.callMethod
                  "destroyRecord"
                  list.item
                  (concat "Successfully deleted role " list.item.id)
                  (concat "There was an error deleting the role " list.item.id)
                  (action "refresh")
                )
              }}
              data-test-scope-delete="true"
            />
          </MenuLoader>
        {{/if}}
      </Item.menu>
    </ListItem>
  {{else}}
    <ListItem as |Item|>
      <Item.content>
        There are no roles that match
        {{this.filter}}, press
        <kbd>ENTER</kbd>
        to add one.
      </Item.content>
    </ListItem>
  {{/if}}
</ListView>